package com.huayue.service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huayue.dao.*;
import com.huayue.entity.Customer;
import com.huayue.entity.RoomDeposit;
import com.huayue.entity.RoomRealOrder;
import com.huayue.entity.RoomRealOrderDetails;
import com.huayue.util.DateHelper;
import com.mysql.DBHelper;

/**
 * 入住订单
 */
@Service
public class RoomRealOrderService extends BaseService<RoomRealOrder> {

    @Autowired
    RoomRealOrderDao dao;

    @Autowired
    RoomRealOrderDetailsDao rrodDao;

    @Autowired
    CustomerDao customerDao;
    @Autowired
    RoomDepositDao depositDao;
    @Autowired
    RoomStatusDao roomStatusDao;

    @Override
    public BaseDao<RoomRealOrder> getDao() {
        return dao;
    }

    // /*
    // *修改订单状态
    // */
    // public int checkIs_status(RoomRealOrder room_real_order)
    // {
    // return dao.changeIs_status(room_real_order);
    // }

    // 查询空房间
    public List<Map<String, Object>> findNullfrom() {
        return dao.findNullfrom();
    }

    // 查询空房间
    public List<Map<String, Object>> findfrom() {
        return dao.findfrom();
    }

    /*
     *查找最大id
     */

    public int findMaxId() {
        int i = 0;
        try {
            i = dao.findMaxId();
        } catch (Exception e) {
            i = 0;
        }
        return i;
    }

    /**
     * 添加入住订单信息
     * 
     * @param room_real_order
     * @param details
     * @return
     */
    public boolean addDetails(RoomRealOrder room_real_order, List<RoomRealOrderDetails> details, Customer customer,
        RoomDeposit deposit) {

        // 计算天数，入住订单totalprice总价需要
        int day = DateHelper.getDateRangeNumber(room_real_order.getRealInTime(), room_real_order.getRealOutTime());
        if (day == 0) {
            day = 1;
        }

        // 成功数量
        int success = 0;
        // 计算总金额
        BigDecimal price = new BigDecimal("0");

        for (RoomRealOrderDetails orderDetails : details) {
            price = price.add(orderDetails.getPrice());
        }
        price = price.multiply(BigDecimal.valueOf(day));
        // 将顾客信息添加到数据库 --customer表
        customerDao.insert(customer);

        // 完善订单信息
        room_real_order.setTotalprice(price);
        // 将订单信息添加到数据库
        success = dao.insert(room_real_order);
        if (success == 1) {

            // 将订单详情添加到数据库
            for (RoomRealOrderDetails orderDetails : details) {
                orderDetails.setDays(Integer.valueOf(room_real_order.getDays().intValue()));
                // 添加顾客/订单详情
                success += rrodDao.insert(orderDetails);
                // 修改房态
                dao.updateRoom_status(room_real_order, orderDetails);
            }
            // 将押金订单信息添加到数据库中
            for (RoomRealOrderDetails orderDetails : details) {
                deposit.setRoom_id(orderDetails.getRoom_id());
                depositDao.insert(deposit);
            }
        }
        if (success == (details.size() + 1)) {
            return true;
        }
        return false;
    }

    /**
     * 查询订单详情
     * 
     * @param id
     * @return
     */
    public List<Map<String, Object>> findDetails(String id) {
        return rrodDao.findDetails(id);
    }

    /**
     * 查询room_type房间类型所对应的room房间
     * 
     * @param id
     * @return
     */

    public List<Map<String, Object>> findIdRoom(int id) {
        String sql = "select * from room where id = ?,and is_deleted =0";
        return DBHelper.executeQuery(sql, id);
    }

    /**
     * @Author: 陈世纪
     * @Date: 1:17 2020/3/10
     * @Description: 查看详情时，room房间的押金 --- 暂时没有用上
     */
    public List<Map<String, Object>> findRoom_deposit_price(String id) {
        return dao.findRoom_deposit_price(id);
    }

    /**
     * 根据房间id查询该房间的入住天数与日期
     * 
     * @return
     */
    public RoomRealOrder findDaysAndInTimeByRoomId(Integer roomId) {
        return dao.findDaysAndInTimeByRoomId(roomId);
    }

    public Map<String, Object> findRoomRenew(Integer roomId) {
        return dao.findRoomRenew(roomId);
    }

    /**
     * 续房
     * 
     * @return
     */
    public boolean renewRoom(Integer roomId, Integer days, BigDecimal addPrice) {
        // 修改住房详情的天数/补缴金额
        RoomChangeDao dao = new RoomChangeDao();
        int oldId = dao.findGenRoomDetailByRoomId(roomId);
        int i = rrodDao.updateRoomRealDetailsDays(oldId, days, addPrice);
        // 修改房态表中的退房时间
        int j = roomStatusDao.addDaysByRoom(days);
        if (i > 0 && j > 0) {
            return true;
        }
        return false;
    }

}
